<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="成果名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入成果名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:demand:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="achievementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id" width="50"/>
      <el-table-column label="成果名" align="center" prop="name"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="线条" align="center" prop="line" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_line" :value="scope.row.line"/>
        </template>
      </el-table-column>
      <el-table-column label="获奖时间" align="center" prop="awardTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.awardTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="clickNum" width="60"/>
      <el-table-column label="状态" align="center" prop="state" width="70">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="ingDetailClick(scope.row)"
            v-hasPermi="['project:achievement:list']"
          >查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 查看详情对话框 -->
    <el-dialog :title="title" :visible.sync="detailOpen" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="需求名:">{{ form.name }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="线条:">{{ form.line }} / {{ lineFormat(form) }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间:">{{ form.createTime }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核人1:">{{ form.checker1Name }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核人2:">{{ form.checker2Name }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="内容:">
              <editor v-model="form.description" :min-height="192" :read-only="true"/>
            </el-form-item>
          </el-col>


        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改需求对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="主题" prop="name">
          <el-input v-model="form.name" placeholder="请输入主题"/>
        </el-form-item>
        <el-form-item label="线条" prop="line">
          <el-select v-model="queryParams.line" placeholder="请选择线条" clearable>
            <el-option
              v-for="dict in dict.type.project_line"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="奖项" prop="prize">
          <el-select v-model="queryParams.line" placeholder="请选择奖项" clearable>
            <el-option
              v-for="dict in dict.type.project_prize"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="获奖时间" prop="awardTime">
          <el-date-picker clearable
                          v-model="form.awardTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择获奖时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创新点" prop="innovation">
          <el-input v-model="form.innovation" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.description" :min-height="192"/>
        </el-form-item>
        <el-form-item label="文件" prop="file">
          <file-upload v-model="form.file"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addAchievement,
  delAchievement,
  getAchievement,
  listAchievement, listAchievementByClickNum,
  updateAchievement
} from "@/api/project/achievement";
import {listDemand} from "@/api/project/demand";
import {getTopic, listTopic} from "@/api/project/topic";

export default {
  name: "Demand",
  dicts: ['project_state', 'project_line', 'project_prize'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 需求表格数据
      achievementList: [],
      // 弹出层标题
      title: "",
      detailOpen: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: 3,
        line: null,
        prize: null,
        awardTime: null,
        userId: null,
        otherPrize: null,
        state: 2,
        needNum: null,
        isShow: 1,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 线条字典类型翻译
    lineFormat(row, column) {
      return this.selectDictLabel(this.dict.type.project_line, row.line);
    },
    /** 查看详情 */
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getAchievement(id).then(response => {
        this.form = response.data;
        this.detailOpen = true;
        this.title = "查看详情";
      });
    },
    ingDetailClick(row) {
      this.$router.push({path: '/ingDetail', query: {"id": row.id, "from": 1}});
    },
    /** 查询需求列表 */
    getList() {
      this.loading = true;
      listAchievementByClickNum(this.queryParams).then(response => {
        this.achievementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.detailOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        desc: null,
        line: null,
        keyTask1: null,
        keyTask2: null,
        keyTask3: null,
        prize: null,
        awardTime: null,
        file: null,
        innovation: null,
        userId: null,
        clickNum: null,
        otherPrize: null,
        checker1: null,
        checker2: null,
        state: null,
        needNum: null,
        isShow: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加需求";
    },

    /** 送审  */
    handleCheck(row) {
      /**
       *
       * 需要补充代码
       * */
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAchievement(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改需求";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAchievement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAchievement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除需求编号为"' + ids + '"的数据项？').then(function () {
        return delAchievement(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/achievement/export', {
        ...this.queryParams
      }, `achievement_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
